﻿using ArtMath.Core.Data;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ArtMath.Component.Annotation
{
    /// <summary>
    /// 纯文本
    /// </summary>
    [Serializable]
    public class PlainText
    {
        string text;
        Font font = SystemFonts.DefaultFont;
        Color color = SystemColors.WindowText;
        [NonSerialized]
        StringFormat sf = new StringFormat();
        Size size;
        ContentAlignment align;
        public PlainText(string text)
        {
            Text = text;
            Alignment = ContentAlignment.TopLeft;
        }
        void reCalcSize()
        {
            size = TextRenderer.MeasureText(Text, Font);
            size.Width += (int)font.Size;
        }
        public string Text
        {
            get { return text; }
            set
            {
                text = value;
                reCalcSize();
            }
        }
        public Font Font
        {
            get { return font; }
            set
            {
                font = value;
                reCalcSize();
            }
        }
        public Color Color
        {
            get { return color; }
            set { color = value; }
        }
        public Size Size
        {
            get { return size; }
        }
        public ContentAlignment Alignment
        {
            get { return align; }
            set
            {
                align = value;
                switch (align)
                {
                    case ContentAlignment.TopLeft:
                        sf.Alignment = sf.LineAlignment = StringAlignment.Near;
                        break;
                    case ContentAlignment.TopCenter:
                        sf.Alignment = StringAlignment.Center;
                        sf.LineAlignment = StringAlignment.Near;
                        break;
                    case ContentAlignment.TopRight:
                        sf.Alignment = StringAlignment.Far;
                        sf.LineAlignment = StringAlignment.Near;
                        break;
                    case ContentAlignment.MiddleLeft:
                        sf.Alignment = StringAlignment.Near;
                        sf.LineAlignment = StringAlignment.Center;
                        break;
                    case ContentAlignment.MiddleCenter:
                        sf.Alignment = sf.LineAlignment = StringAlignment.Center;
                        break;
                    case ContentAlignment.MiddleRight:
                        sf.Alignment = StringAlignment.Far;
                        sf.LineAlignment = StringAlignment.Center;
                        break;
                    case ContentAlignment.BottomLeft:
                        sf.Alignment = StringAlignment.Near;
                        sf.LineAlignment = StringAlignment.Far;
                        break;
                    case ContentAlignment.BottomCenter:
                        sf.Alignment = StringAlignment.Center;
                        sf.LineAlignment = StringAlignment.Far;
                        break;
                    case ContentAlignment.BottomRight:
                        sf.Alignment = sf.LineAlignment = StringAlignment.Far;
                        break;
                }
            }
        }
        public void Paint(Graphics gra)
        {
            Paint(gra, new PointD(0, 0));
        }
        public void Paint(Graphics gra, PointD location)
        {
            Paint(gra, new RectangleF(location.ToPointF(), Size));
        }
        public void Paint(Graphics gra, RectangleF layoutRegion)
        {
            gra.DrawString(Text, Font, new SolidBrush(Color), layoutRegion, sf);
        }
    }
}
